<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              ref="userFrom"
              :model="userInfo"
              :rules="rules"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width: 300px" />
              </el-form-item>
              <el-form-item label="登陆账号/手机号:" prop="mobile">
                <el-input
                  v-model="userInfo.mobile"
                  disabled
                  style="width: 300px"
                />
              </el-form-item>
              <el-form-item label="密码:" prop="nowpassword">
                <el-input
                  v-model="userInfo.nowpassword"
                  style="width: 300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser"> 更新 </el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 打印 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 这里使用的了 Vue.js 中的组件component (动态组件) 指定一个已注册的组件 -->
            <!-- 可以对 :is中绑定的值(为一个储存组件的变量)进行更换实现组件切换 -->
            <!-- <el-button type="primary" @click="userComponent = 'el-button'"> 切换组件 </el-button>
            <el-button type="primary" @click="userComponent = 'UserInfo'"> 切回组件 </el-button> -->
            <component :is="userComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!-- 打印 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="JobComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import UserInfo from './userInfo.vue'
import JobInfo from './jobInfo'
export default {
  name: 'Detail',
  components: {
    UserInfo,
    JobInfo
  },
  props: {},
  data() {
    return {
      userComponent: 'UserInfo',
      JobComponent: 'JobInfo',
      userId: this.$route.params.id, // :id 为params参数 ?id 为query参数
      userInfo: {
        username: '',
        mobile: '',
        nowpassword: '' // 获取的密码为一段加密的字段,所以这里为了区别元素数据中的password
      },
      rules: {
        username: [
          {}
        ],
        nowpassword: [
          {}
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      this.userInfo = await getUserDetailById(this.userId)
    },
    saveUser() {
      this.$refs.userFrom.validate().then(async () => {
        await saveUserDetailById({ ...this.userInfo, password: this.userInfo.nowpassword })
        this.$message.success('修改密码成功')
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
